package tv.youxing.android.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.youxing.android.R;
import tv.youxing.android.adapter.MyMessageAdapter;
import tv.youxing.android.entity.MyMessageModel;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

/**
 * 我的消息
 */
public class MyMessageActivity extends BaseActivity {

    @BindView(R.id.lv)
    ListView lv;

    MyMessageModel allData;
    List<MyMessageModel.ResultBean.DataBean> listData = new ArrayList<>();
    private MyMessageAdapter mMyMessageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        ButterKnife.bind(this);

        initView();
        loadData();
        initAdapter();
    }

    private void initView() {
    }

    private void loadData() {
        //post请求简洁版实现
        showProgress("加载中");
        HttpParams params = new HttpParams();
        params.putHeaders("Authorization", "Bearer " + ShareUtils.getString(this, AppConst.TOKEN, ""));

        RxVolley.post(StaticClass.myMessage, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                allData = GsonUtils.GsonToBean(t, MyMessageModel.class);

                if (allData.getCode() == AppConst.TOKENERROR) {
                    MyMessageActivity.this.startActivity(new Intent(MyMessageActivity.this,LoginActivity.class));
                    return;
                }

                listData.addAll(allData.getResult().getData());
                mMyMessageAdapter.notifyDataSetChanged();
                hideProgress();
            }
        });
    }

    void initAdapter() {
        mMyMessageAdapter = new MyMessageAdapter(MyMessageActivity.this, listData);
        lv.setAdapter(mMyMessageAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Intent intent = new Intent(MyMessageActivity.this,DynamicDetailsActivity.class);
//                intent.putExtra("resource_type",2);
//                intent.putExtra("resource_id",listData.get(i).getResource_id());
//
//                startActivity(intent);

            }
        });
    }



}
